//
//  ResponseException.swift
//  XBasicKit
//
//  Created by x on 2024/8/29.
//

import Foundation


public struct XResult<T: Any> {
    var code: Int?
    var success: Bool?
    var msg: String?
    var data: T?
}

public struct ErrorModel: Codable {
    var code: String?
    var message: String?
    var field: String?
    var resource: String?
}

public struct ErrorResponse: Codable {
    var message: String?
    var errors: [ErrorModel] = []
    var documentationUrl: String?

    func detail() -> String {
        return errors.map { $0.message ?? "" }
            .joined(separator: "\n")
    }
}

public enum ApiError: Error {
    case serverError(response: ErrorResponse)

    var title: String {
        switch self {
        case .serverError(let response): return response.message ?? ""
        }
    }

    var description: String {
        switch self {
        case .serverError(let response): return response.detail()
        }
    }
    
//    func checkStatus() {
//        switch self {
//        case .serverError(let response):
//            
//        }
//    }
}

